package com.suge.send.tcp;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.jasson.mas.api.ApiClientFactory;
import com.jasson.mas.api.common.ConnectStatus;
import com.jasson.mas.api.sms.SmsApiClient;
import com.jasson.mas.api.sms.SmsApiClientHandler;
import com.jasson.mas.api.smsapi.MsgFmt;
import com.jasson.mas.api.smsapi.SmsSendRequest;
import com.jasson.mas.api.smsapi.SmsSendResponse;
import com.jasson.mas.api.smsapi.SmsType;
import com.suge.order.dao.dataobject.SmsOrderDO;
import com.suge.order.manager.SmsOrderManager;

public class SmsMessageTCPSendClient {

	private static final Logger logger = LoggerFactory.getLogger(SmsMessageTCPSendClient.class);
	
	SmsApiClient smsApiClient = null;

	private SmsOrderManager smsOrderManager;
	//long mobile = 13819148928;
	
	String appID="api";//API ID
	String appPwd="api@12345";//API mima
	String content="亲爱的suge123，您在蓝月亮购买的宝贝已经通过申通快递发货, 谢谢惠顾。如果有问题，请及时和我们小二联系."; 
	String xcode="123";//
	int masApiPort=61616; 
	String masIP="221.178.238.50";
	
	public boolean send(){
		
		SmsOrderDO smsOrderDO = smsOrderManager.getSmsOrderByOrderId(6L);
		//SmsApiClientHandlerImpl
		SmsApiClientHandler smsHandler = new SmsApiClientHandlerImpl(smsOrderDO,smsOrderManager); 
		
		try { 
			smsApiClient =  ApiClientFactory.createSmsApiClient(smsHandler, masIP, masApiPort, appID, appPwd);
			//
			smsApiClient.setAutoConnect (true); 
			smsApiClient.setReConnectInterval (60); //
			smsApiClient.setConnectTimeout(100000); //
			smsApiClient.setSendTimeout(1000000); //
			smsApiClient.start();
			ConnectStatus connectStatus= smsApiClient.getConnStatusIAGW();
			if (!ConnectStatus.Connect.equals(connectStatus)) { 
				return false;
			} 
			SmsSendRequest smsSendRequest = new SmsSendRequest();
			List<String> list = new ArrayList<String>(); 

			list.add(String.valueOf("13967128961"));
			//list.add(String.valueOf("13819453953"));
			smsSendRequest.destAddrs = list; 
			smsSendRequest.validTime = 10000; // 
			smsSendRequest.xCode = xcode; //
			smsSendRequest.message = content; // 
			smsSendRequest.msgFormat =MsgFmt.GB2312; //
			smsSendRequest.isNeedReport = true; //
			smsSendRequest.priority = 1; //
			smsSendRequest.type = SmsType.Normal; 
			smsSendRequest.appID = appID; //
			SmsSendResponse smsSendResponse = smsApiClient.sendSms(smsSendRequest); 
			logger.error("API RequestID:" + smsSendResponse.requestID); 
			} 
			catch (Exception e) { 
				logger.error("API调用:" + e.getMessage()+"||"+e.toString());
				return false; 
			}

		return true;
	}

	public void setSmsOrderManager(SmsOrderManager smsOrderManager) {
		this.smsOrderManager = smsOrderManager;
	}

	public SmsOrderManager getSmsOrderManager() {
		return smsOrderManager;
	}

}
